package cn.wfu.rlrjds.app.repaire.service.ipml;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.wfu.rlrjds.app.bean.RepaireBean;
import cn.wfu.rlrjds.app.bean.RepaireSearch;
import cn.wfu.rlrjds.app.repaire.dao.IRepaireDao;
import cn.wfu.rlrjds.app.repaire.domain.Repaire;
import cn.wfu.rlrjds.app.repaire.service.IRepaireService;
import cn.wfu.rlrjds.utils.GsonUtils;

import com.google.gson.reflect.TypeToken;

@Service
@Transactional(readOnly = true)
public class RepaireServiceIpml implements IRepaireService {

	@Resource
	private IRepaireDao repaireDao;

	@Override
	@Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
	public void save(Repaire repaire) {
		// TODO Auto-generated method stub
		this.repaireDao.save(repaire);
	}

	/**
	 * 增加对维修员的评价
	 */
	@Override
	@Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
	public void addEvaluate(Map<String, String> map) {
		// TODO Auto-generated method stub
		this.repaireDao.addEvaluate(map);
		this.repaireDao.updateConfirmStatus(map);
	}

	/**
	 * 查找未维修的记录
	 */
	@Override
	public List<RepaireBean> getAllRepaireByRepaireStatus() {
		// TODO Auto-generated method stub
		List<Repaire> repaires = this.repaireDao.getAllRepaireByRepaireStatus();

		List<RepaireBean> beans = getBeans(repaires);
		return beans;
	}

	/**
	 * 将单个vo对象转换成单个po对象
	 * 
	 * @param repaire
	 * @return
	 */
	private RepaireBean getBean(Repaire repaire) {
		// TODO Auto-generated method stub
		List<Repaire> repaires = new ArrayList<Repaire>();
		repaires.add(repaire);
		List<RepaireBean> beans = getBeans(repaires);
		return beans.get(0);
	}

	/**
	 * 将多个vo对象转换成多个po对象
	 * 
	 * @param selectList
	 * @return
	 */
	private List<RepaireBean> getBeans(List<Repaire> selectList) {
		// TODO Auto-generated method stub
		List<RepaireBean> beans = new ArrayList<RepaireBean>();
		for (Repaire repaire : selectList) {
			try {
				String imgUrl = repaire.getImgUrl();
				Type type = new TypeToken<List<String>>() {
				}.getType();
				List<String> imgUrls = GsonUtils.fromJson(imgUrl, type);
				List<String> goods = GsonUtils.fromJson(repaire.getGoods(),
						type);
				repaire.setGoods(null);
				repaire.setImgUrl(null);
				RepaireBean bean = new RepaireBean();
				ConvertUtils.register(new DateConverter(null),
						java.util.Date.class);
				BeanUtils.copyProperties(bean, repaire);
				bean.setImgUrl(imgUrls);
				bean.setGoods(goods);
				beans.add(bean);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return beans;
	}

	/**
	 * 增加用户评价
	 */
	@Override
	@Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
	public void toRepair(Repaire repaire) throws RepaireException {
		// TODO Auto-generated method stub
		Repaire _repaire = this.repaireDao.findById(repaire.getRid());
		if (_repaire.getRepaireStatus() == 1) {
			throw new RepaireException("已经被其他管理员接单");
		}
		this.repaireDao.updateRepairStatus(repaire);
	}

	/**
	 * 查找用户
	 */
	@Override
	public List<RepaireBean> list(RepaireSearch repaireSearch) {
		// TODO Auto-generated method stub
		List<Repaire> repaires = this.repaireDao.list(repaireSearch);
		List<RepaireBean> beans = getBeans(repaires);
		return beans;
	}

	/**
	 * 查找一名符合条件的用户
	 */
	@Override
	public RepaireBean findOne(RepaireSearch repaireSearch) {
		// TODO Auto-generated method stub
		List<RepaireBean> repaireBeans = this.list(repaireSearch);
		if (repaireBeans != null && !repaireBeans.isEmpty()
				&& repaireBeans.size() == 1) {
			return repaireBeans.get(0);
		}
		return null;
	}

	/**
	 * 完成订单
	 */
	@Override
	@Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
	public void finishRepaire(Repaire repaire) {
		// TODO Auto-generated method stub
		this.repaireDao.updateRepairStatus(repaire);
	}

	/**
	 * 查找所有订单
	 */
	@Override
	public List<RepaireBean> list() {
		// TODO Auto-generated method stub
		return this.list(null);
	}

	/**
	 * 通过主类型超查找数量
	 */
	@Override
	public List<Integer> findTypeCount() {
		// TODO Auto-generated method stub
		return this.repaireDao.findTypeCount();
	}

	@Override
	public List<Integer> findApartmentGoodsCount() {
		// TODO Auto-generated method stub
		return this.repaireDao.findApartmentGoodsCount();
	}

	@Override
	public List<Integer> findApartmentNetCount() {
		// TODO Auto-generated method stub
		return this.repaireDao.findApartmentNetCount();
	}

	@Override
	public List<Integer> findSchoolGoodsCount() {
		// TODO Auto-generated method stub
		return this.repaireDao.findSchoolGoodsCount();
	}

	@Override
	@Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
	public void deleteRepaireByIds(String[] ids) {
		// TODO Auto-generated method stub
		this.repaireDao.updateIsdeleteByIds(ids);
	}

}
